package com.crm.dao.impl;

import com.crm.dao.RepairDao;
import com.crm.model.PageBean;
import com.crm.model.Repair;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository("repairDao")
public class RepairDaoImpl implements RepairDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(Repair repair) {
        String sql = "insert into t_equipmentRepair values(null,?,?,null,now(),null,0,1)";
        jdbcTemplate.update(sql, repair.getEquipmentId(), repair.getUserMan());
    }

    @Override
    public List<Repair> list(PageBean pageBean, Repair s_repair) {
        StringBuffer sb = new StringBuffer("select * from t_equipmentRepair t1,t_equipment t2 where t1.equipmentId=t2.id");
        if (s_repair != null) {
            if (StringUtils.isNotEmpty(s_repair.getEquipmentName())) {
                sb.append(" and t2.name like '%" + s_repair.getEquipmentName() + "%'");
            }
            if (s_repair.getFinishState() != null) {
                sb.append(" and t1.finishState=" + s_repair.getFinishState());
            }
        }
        if (pageBean != null) {
            sb.append(" limit " + pageBean.getStart() + "," + pageBean.getPageSize());
        }
        final List<Repair> repairList = new ArrayList<Repair>();

        jdbcTemplate.query(sb.toString(), new Object[]{}, new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                Repair repair = new Repair();
                repair.setId(rs.getInt("id"));
                repair.setEquipmentId(rs.getInt("equipmentId"));
                repair.setEquipmentName(rs.getString("name"));
                repair.setEquipmentNo(rs.getString("no"));
                repair.setUserMan(rs.getString("userMan"));
                repair.setRepairMan(rs.getString("repairMan"));
                try {
                    repair.setRepairTime(DateUtils.parseDate(rs.getString("repairTime"), "yyyy-MM-dd HH:mm:ss"));
                    repair.setRepairTime(DateUtils.parseDate(rs.getString("finishTime"), "yyyy-MM-dd HH:mm:ss"));
                } catch (Exception exception) {
                    System.out.println("����ת������");
                }
                repair.setState(rs.getInt("state"));
                repairList.add(repair);
            }
        });

        return repairList;
    }

    @Override
    public int count(Repair s_repair) {
        StringBuffer sb = new StringBuffer("select count(*) from t_equipmentRepair t1,t_equipment t2 where t1.equipmentId=t2.id");
        if (s_repair != null) {
            if (StringUtils.isNotEmpty(s_repair.getEquipmentName())) {
                sb.append(" and t2.name like '%" + s_repair.getEquipmentName() + "%'");
            }
            if (s_repair.getFinishState() != null) {
                sb.append(" and t1.finishState=" + s_repair.getFinishState());
            }
        }
        return jdbcTemplate.queryForObject(sb.toString(), Integer.class);
    }

    @Override
    public void update(Repair repair) {
        String sql = "update t_equipmentRepair set repairMan=?,finishTime=now(),state=?,finishState=2 where id=?";
        jdbcTemplate.update(sql, repair.getRepairMan(), repair.getState(), repair.getId());
    }

}
